1 module jupyter.wire.log;
2 
3 
4 version(Windows) {
5     extern(Windows) void OutputDebugStringW(const wchar* fmt) @nogc nothrow;
6 }
7 
8 
9 /**
10    Polymorphic logging function.
11    Prints to the console when unit testing and on Linux,
12    otherwise uses the system logger on Windows.
13  */
14 void log(
15     string file = __FILE__,
16     size_t line = __LINE__,
17     string funcName = __FUNCTION__,
18     string prettyFuncName = __PRETTY_FUNCTION__,
19     string moduleName = __MODULE__,
20      A...
21     )
22     (
23         auto ref A args,
24     )
25     @trusted
26 {
27     try {
28         version(unittest) {
29             version(Have_unit_threaded) {
30                 import unit_threaded: writelnUt;
31                 writelnUt(args);
32             } else {
33                 import std.stdio: writeln;
34                 writeln(args);
35             }
36         } else version(JupyterLogStdout) {
37             import std.experimental.logger: trace;
38             trace!(line, file, funcName, prettyFuncName, moduleName)(args);
39 
40         } else version(JupyterLogFile) {
41 
42             import std.experimental.logger.filelogger: FileLogger;
43 
44             static FileLogger fileLogger;
45 
46             if(fileLogger is null) {
47                 fileLogger = new FileLogger("/tmp/jupyter.txt");
48             }
49 
50             fileLogger.log(args);
51 
52         } else version(Windows) {
53             import std.conv: text, to;
54             scope txt = text(args);
55             scope wtxt = txt.to!wstring;
56             OutputDebugStringW(wtxt.ptr);
57         } else {
58             import std.experimental.logger: trace;
59             trace!(line, file, funcName, prettyFuncName, moduleName)(args);
60         }
61     } catch(Exception e) {
62         import core.stdc.stdio: printf;
63         printf("Error - could not log\n");
64     }
65 }